//
//  MainViewController.swift
//  weiboLQ
//
//  Created by 半缘君 on 15/12/6.
//  Copyright © 2015年 banyuanjun.cn. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //添加子控制器
        addChildViewControllers()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        //中间按钮
        addComposedButton()
    }
    
    func addComposedButton () {
        //创建按钮
        let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_hightlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        //设置大小
        btn.sizeToFit()
//        btn.center = tabBar.center
        //计算一个item的宽度
        let w = tabBar.subviews[0].bounds.size.width - 1
        //设置中间按钮的frame
        btn.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
        //添加到tabBar
        tabBar.addSubview(btn)
        
        btn.addTarget(self, action: "clickComposeStatus", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    //点击中间按钮打开发布微博
   @objc func clickComposeStatus () {
    
    // 判断用户是否登录
    var vc: UIViewController
    if UserAccountViewModel.shareAccountModel.isLogin {
        vc = ComposeViewController()
    } else {
        vc = OAuthViewController()
    }

    let nav = UINavigationController(rootViewController: vc)
    
        presentViewController(nav, animated: true, completion: nil)
    }

}


//MARK: - 添加子控制器
extension MainViewController{
    
    func addChildViewControllers() {
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(DIscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    
    func addChildViewController(vc:UIViewController,title:String,imageName:String) {
        //添加HomeVc
//        let vc = HomeTableViewController()
        //标题
         vc.title = title
//        UIColor.orangeColor()
        //设置渲染颜色
//        tabBar.tintColor = UIColor.orangeColor()
       

        //图片
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage =
            UIImage(named: imageName + "_highlighted")
        
         //包装导航控制器
        let nv = UINavigationController(rootViewController: vc)
        //添加子控制器
        addChildViewController(nv)
//        tabBarItem.tintColor = UIColor.orangeColor()
    }
    
}
